#include <sys_head.h>

int my_file_stat(const char * file_name);

int my_dir_stat(const char * dirpath);

int main(int argc, char const *argv[])
{
    if (2 != argc)
    {
        printf("参数错误:%s dirname\n" , argv[0]);
        return 0;
    }
    const char * dirpath = argv[1];
    my_dir_stat(dirpath);
    return 0;
}

int my_dir_stat(const char * dirpath)
{
    char file_name[1024];
    //打开文件
    DIR * dir = opendir(dirpath);
    if (NULL == dir)
    {
        ERRLOG("opendir erron");
    }
    struct dirent * read_dir;
    while ((read_dir = readdir(dir)) != NULL)
    {
        memset(file_name,0,sizeof(file_name));
        if (strncmp(read_dir->d_name,".",1))//|| strncmp(read_dir->d_name,"..",1)
        {
            sprintf(file_name,"./%s/%s",dirpath,read_dir->d_name);
            struct stat statbuf;
            if (stat(file_name,&statbuf) == -1)
            {
                perror("stat");
            }
            
            if (S_ISDIR(statbuf.st_mode))
            {
                my_dir_stat(file_name);
            }
            else
            {
                my_file_stat(file_name);
            }
            
        }
    }
    closedir(dir);
}

int my_file_stat(const char * file_name)
{
    struct stat statbuf;
    stat(file_name,&statbuf);
    //获取文件名
    int byt_n;
    for (size_t i = 0; file_name[i] != '\0'; i++)
    {
        if ('/' == file_name[i])
        {
            byt_n = i;
        }
    }
    //文件类型
    char permissions[11] = "----------";

    if (S_ISREG(statbuf.st_mode))
    {
        permissions[0] = '-';
    }else if (S_ISDIR(statbuf.st_mode))
    {
        permissions[0] = 'd';
    }else if (S_ISCHR(statbuf.st_mode))
    {
        permissions[0] = 'c';
    }else if (S_ISBLK(statbuf.st_mode))
    {
        permissions[0] = 'b';
    }else if (S_ISFIFO(statbuf.st_mode))
    {
        permissions[0] = 'p';
    }else if (S_ISLNK(statbuf.st_mode))
    {
        permissions[0] = 'l';
    }else if (S_ISSOCK(statbuf.st_mode))
    {
        permissions[0] = 's';
    }
    
    //文件权限
    if (statbuf.st_mode & S_IRUSR)
    {
        permissions[1] = 'r';
    }
    if (statbuf.st_mode & S_IWUSR)
    {
        permissions[2] = 'w';
    }
    if (statbuf.st_mode & S_IXUSR)
    {
        permissions[3] = 'x';
    }
    if (statbuf.st_mode & S_IRGRP)
    {
        permissions[4] = 'r';
    }
    if (statbuf.st_mode & S_IWGRP)
    {
        permissions[5] = 'w';
    }
    if (statbuf.st_mode & S_IXGRP)
    {
        permissions[6] = 'x';
    }
    if (statbuf.st_mode & S_IROTH)
    {
        permissions[7] = 'r';
    }
    if (statbuf.st_mode & S_IWOTH)
    {
        permissions[8] = 'w';
    }
    if (statbuf.st_mode & S_IXOTH)
    {
        permissions[9] = 'x';
    }
    
    //所属者姓名
    struct passwd * user;
    user = getpwuid(statbuf.st_uid);

    //用户组名称
    struct group * Group;
    Group = getgrgid(statbuf.st_gid);

    //时间
    struct tm * time_stu = localtime(&statbuf.st_mtime);

    printf("%s %ld %s %s %ld %d月 %d %d:%d %s\n",
            permissions , statbuf.st_nlink , 
            user->pw_name , Group->gr_name , statbuf.st_size , 
            time_stu->tm_mon + 1 , time_stu->tm_mday ,time_stu->tm_hour , time_stu->tm_min , &file_name[byt_n + 1]);
    return 0;
}